IF exists(SELECT * FROM dbo.sysobjects WHERE name='geefKlassenVakkenSamenMet' AND xtype='FN') 
BEGIN
	DROP FUNCTION geefKlassenVakkenSamenMet
END
GO

create function geefKlassenVakkenSamenMet
(@klas varchar(5))
returns @resultaat table (klas varchar(5),vak char(2))
begin
declare @vakken table(vak char(2))

declare deCursor cursor local for 
  select distinct vakcode
  from leraars..ambtsverdeling where klas = @klas
for read only

open deCursor
declare @vak char(2)

fetch next from deCursor into @vak
while @@fetch_status=0
begin
  insert into @resultaat(klas,vak)
  select klas,@vak from leraars.dbo.geefKlassenSamenMet(@klas,@vak)
  fetch next from deCursor into @vak
end

deallocate deCursor

return
end